<!DOCTYPE html>
<!--

save.html

code for saving thought patches locally

Copyright (C) 2009, Chris Allert, All rights reserved.

This program is free software; you can redistribute it and/or modify it under 
the terms of the GNU General Public License as published by the Free Software 
Foundation; either version 3 of the License, or (at your option) any later 
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY 
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with 
this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
Place, Suite 330, Boston, MA 02111-1307 USA

The latest version of this file can be downloaded from
http://thought-patches.sourceforge.net/

-->
<html>
	<head>
		<title>patches</title>
<link rel="apple-touch-icon" href="../../thought-patches.png">
<meta content="width=device-width; initial-scale=1.0;  maximum-scale=1.0; user-scalable=0;" name="viewport" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no" />
<style>
	body { padding:0px; margin:0px; color: white; background-color: black; font-size: 16px }
	input { 
		font-size:16px;
		padding: 0px;
	}
	select { 
		font-size:16px;
		padding: 0px;
	}
	* {font-family: sans-serif}
	form { padding:0px; margin:0px; }
</style>
		<script src="../../lib/query.js"></script>
		<script>

			var patches = [];

			var labels = {
				"cartwheel-tiling": "cartwheel",
				"hypotrochoid": "hypotrochoid",
				"epitrochoid": "epitrochoid",
				"repeating-decimal": "repeating"
			};

			function compare_patches(a, b) {
				var ka = parseInt(a);
				var kb = parseInt(b);
				return b - a;
			}

			function recall_patches() {
				patches = new Array();
				var a = document.cookie.split(';');
				var d = new Date();
				d.setDate(d.getDate() + 365);
				for (var i in a) {
					if ('' == a[i]) { 
						continue; 
					}

					document.cookie = a[i] + 
					";expires=" + d.toUTCString();

					var p = a[i].split('=');
					var k = decodeURIComponent(p[0]);
					var v = decodeURIComponent(p[1]);
					var va = v.split("&");
					var o = new Array();
					o['search'] = v;
					for (var j in va) {
						var vp = va[j].split('=');
						o[decodeURIComponent(vp[0])] =
						decodeURIComponent(vp[1]);
					}
					patches[k] = o;
				}
			}

			function escape_html(s) {
				if (null == s) { return "null" }
				var o = s.replace('&', '&amp;');
				o = o.replace('<', '&lt;');
				o = o.replace('>', '&gt;');
				o = o.replace('"', '&quot;');
				o = o.replace("'", '&apos;');
				return o;
			}

			function show_patch(k) {
				var p = patches[k]['patch'];
				if (null == patches[k]['title']) { return ''; }
				var o = '<tr><td><img src="../' + p + '/' + p + '.png"></td><td>' + 
					escape_html(patches[k]['title']) + 
					'<br /><input type="button" onclick="go_to(\'../' + p + '/' + p + '.html?' + patches[k]['search'] + '\')" value="view" /><input type="button" onclick="go_to(\'../share/share.html?' + patches[k]['search'] + '\')" value="share" /><input type="button" onClick="delete_patch(\'' + k + '\')" value="delete" /></td></tr>';
				return o;
			}

			function show_new_patch(k) {
				return '<tr onclick="go_to(\'../' + k + '/' + k + '.html\')"><td><img src="../' + k + '/' + k + '.png" /></td><td>' + labels[k] + '</td></tr>';
			}

			function show_patches() {
				var o = '<table cellpadding="5">';
				var a = new Array();
				var ap = [
					"cartwheel-tiling",
					"hypotrochoid",
					"epitrochoid",
					"repeating-decimal"
				];
				for (var i in patches) { a.push(i); }
				a.sort(compare_patches);
				a.reverse();
				if (navigator.onLine) {
					for (var i = 0; i < a.length; ++i) {
						o += show_patch(a[i]);
					}
				}
				for (var i = 0; i < ap.length; ++i) {
					o += show_new_patch(ap[i]);
				}
				o += '<tr><td colspan="2">&nbsp;</td></tr>';
				o += '<tr><td colspan="2"><a style="color: white; " target="_blank" href="http://code.google.com/p/thoughtpatches/">view open source</a></td></tr>';
				o += '</table>';
				document.getElementById('listing').innerHTML = o;
			}

			function delete_patch(k) {
				var d = new Date();
				var p = "deleted=true";
				d.setDate(d.getDate() - 1);
				document.cookie = k +
				"=" + encodeURIComponent(p) + 
				";expires=" + d.toUTCString();
				recall_patches();
				show_patches();
			}

			function save_patch() {
				var p = document.location.search.substring(1);
				if (null == p) { return; }
				if ('null' == p) { return; }
				if (0 == p.length) { return; }
				var d = new Date();
				var k = d.valueOf();
				d.setDate(d.getDate() + 365);
				document.cookie = "patch_" + k +
				"=" + encodeURIComponent(p) + 
				";expires=" + d.toUTCString();
			}

			function init() {
				if (0 < document.location.search.substring(1).length) {
					save_patch();
					document.location = "save.html";
					} else {
						recall_patches();
						show_patches();
					}
			}

			function go_to(u) {
				// have to do this so iphone web app doesn't
				// open new safari window
				document.location = u;
			}

		</script>
	</head>
	<body onLoad="init()">
		<div id="listing">saving</div>
	</body>
</html>
